{include file='public/header'}

    <blockquote class="layui-elem-quote">
        采集小说
    </blockquote>
    <div>
        <button class="layui-btn start">开始</button>
    </div>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>{$section_name|default=''}</legend>
    </fieldset>

{include file='public/footer'}
{js href="/static/home/js/store.js" /}
{if condition="$Think.get.act neq 'reload'"}
<script>
    $(function () {
        store.clear();
    });
</script>
{/if}
<script>
    //var $ = layui.jquery; //重点处
    //后面就跟你平时使用jQuery一样
    $(function () {
        $('.start').click(function () {
            // 开始
            store.set('start', 0); // 做个计数器
            // 获取小说列表
            $.get('{:url("get_book_ids")}',function (msg) {
                store.set('book_ids', msg.ids);
                operate_cookie(store.get('book_ids'));
            });
        });
        var book_ids = store.get('book_ids') ? store.get('book_ids') : [];
        if (book_ids.length > 0) {
            store.set('start', 1);  // 做个计数器
            operate_cookie(store.get('book_ids'));
        }
    });

    function operate_cookie(c) {
        console.log(c);
        // 获取一个小说ID
        var book_id = c[0];
        console.log(store.get(book_id + '_section_ids'));
        var sections = store.get(book_id + '_section_ids') ? store.get(book_id + '_section_ids') : [];
        if(sections.length == 0) {
            console.log(book_id);
            //根据小说ID 获取章节列表
            $.get('{:url("get_book_dir")}', {id: book_id}, function (msg) {
                // 更新完之后获取章节列表
                var section_ids = [];
                for (var i in msg) {
                    if (!msg[i].con_id) {
                        section_ids.push(msg[i].id);
                    }
                }
                console.log(section_ids);
                if (section_ids.length == 0) {
                    c.shift();
                    console.log(c);
                    store.set('book_ids', c);
                    window.location.href = '{:url("get_data")}?act=reload';
                } else {
                    store.set(book_id + '_section_ids',  section_ids);
                    get_section_content(book_id, c);
                }
            }, 'json');
        } else {
            get_section_content(book_id, c);
        }
    }

    function get_section_content(book_id, c) {

        console.log(book_id);
        var s = store.get(book_id + '_section_ids');
        console.log(s);

        // 获取一个小说章节ID
        var section_id = s[0];
        s.shift();
        store.set(book_id + '_section_ids', s);
        console.log(s);
        //console.log(new_section_cookie);
        if (store.get('start') > 0 && !s) {
            console.log(book_id);
            c.shift();
            console.log(c);
            store.set('book_ids', c);
        }
        console.log(section_id);
        // 跳转页面
        //console.log('{:url("get_book_content")}?act=reload&id=' + section_id);
        setTimeout(function () {
            window.location.href = '{:url("get_book_content")}?act=reload&id=' + section_id;
        }, 500);
    }
</script>